﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Diagnostics;
using System.Xml.Serialization;



namespace TaskManager
{
    /// <summary>
    /// Interaktionslogik für MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private SystemPerformance systemPerformance;
        private ProcessRetriever processRetriever;
        private int selectedIndex;


        //Handles the killing of a process from the context menu
        private void KillProcessContextMenu_OnClick(object sender, RoutedEventArgs e)
        {
            Process[] process = Process.GetProcesses();
            try
            {
                process[selectedIndex].Kill();
            }
            catch (Exception ex)
            {
                var reslult = MessageBox.Show("Kann nicht geschlossen werden", "Access denied", MessageBoxButton.OK, MessageBoxImage.Warning);

            }

        }

        public MainWindow()
        {
            InitializeComponent();
            systemPerformance = SystemPerformance.CreateNewSystemPerformance();
            processRetriever = ProcessRetriever.CreateNewProcessReciever();


            DataWrapper dw = new DataWrapper();
            dw.Proc = processRetriever;
            dw.Sys = systemPerformance;


            this.DataContext = dw;

            //try
            //{
            // dw.Proc = processRetriever.Processes.w
            //    dw.Proc = processRetriever.Processes.Where(p => p.ProcessName.Contains("calc"));
            //}
            //catch { }


        }

        //changes the value of the processes progressbar when the specified value is below the cpu value
        private void Slider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            var slider = sender as Slider;
            double value = slider.Value;

            if (value < systemPerformance.CPUUsage)
            {
                cpuProgressBar.Foreground = Brushes.Red;
            }
            else { cpuProgressBar.Foreground = Brushes.Green; }
        }

        //provides a search function for a process
        private void btnSearchClick(object sender, RoutedEventArgs e)
        {


        }

        //for entering a string to search for a process
        private void TextBox_Search(object sender, RoutedEventArgs e)
        {



        }

        //take a snapsot of processes and write it in a xml file
        private void btnSnapshotClick(object sender, RoutedEventArgs e)
        {

            XmlSerializer s = new XmlSerializer(typeof(ProcessRetriever));
            s.Serialize(Console.Out, this.processRetriever);
            Console.ReadLine();
            //Serializer.write(this.processRetriever, "test.xml");
        }

        //is used for setting the selection of the process in processlist after it is updated
        private void processLV_selectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ListView processList = (sender as ListView);
            string currentSelectedProcess = processList.SelectedItem as string;
            if (processList.SelectedIndex != -1)
            { this.selectedIndex = processList.SelectedIndex; }

        }


    }
}
